<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Orders extends MY_Controller {
	public $left_menu_min;

	function __construct(){
	parent::__construct();

	// $this->left_menu_min = array("menu" => "教务管理", "curr" => '/offline_course', "list" => array("作业管理" => "/classes/home_work_list", "面授课" => "/course_banke", "班级管理" => "/classes", "学员管理" => "/student", "出勤管理" => "/chuqin/today_class", "老师管理" => "/teacher", "课程分类" => "/offline_course/cate", "课程科目" => "/offline_course", "教室管理" => "/school_room", "校区管理" => "/school_campus"));
	// $this->smarty->assign("left_menu_min", $this->left_menu_min);
	// $this->smarty->assign("menu", 'classes');
		$parent_id = 2;
		//获取school二级菜单
		$this->load->model('School_manage_menu_model');
	    $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
	    // print_r($menu_list);exit;
	    $this->smarty->assign("menu_list", $menu_list);

		$this->left_menu_min = array("menu" => "交易订单", "curr" => '/orders', "list" => $menu_list);
	  	$this->smarty->assign("left_menu_min", $this->left_menu_min);
	  	$this->smarty->assign("menu", 'orders');

	}


	/**
	 * 网课订单列表
	 */
	public function index($status='all', $order_type = 1, $order_num=0, $st = 0, $et = 0, $mobile = 0, $limit = 0)
	{
		$this->offset = 10;
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post();
			$order_num = !empty($post_data['order_num']) ? urldecode($post_data['order_num']) : 0;
			$st = !empty($post_data['st']) ? $post_data['st'] : 0;
			$et = !empty($post_data['et']) ? $post_data['et'] : 0;
			$mobile = !empty($post_data['mobile']) ? $post_data['mobile'] : 0;
		}else{
			$order_num = urldecode($order_num);
			$page_config['uri_segment'] = 9;
		}
		$data = array( 'status' => $status,'order_type' => $order_type, 'order_num' => $order_num,'st' => $st,'et' => $et,'mobile' => $mobile);

		$this->load->model('Order_model');
		$result = $this->Order_model->get_order_list_by_school($limit,$this->offset,$this->user_data['school_id'],$data);
		$this->smarty->assign("order", $result['result']);
		//分页
		$this->load->library('pagination');
		$page_config['base_url'] = '/orders/index/'.$data['status'].'/'.$data['order_type'].'/'.$data['order_num'].'/'.$data['st'].'/'.$data['et'].'/'.$data['mobile'];
		$page_config['total_rows'] = $result['total_count'];
		$page_config['per_page'] = $this->offset;
		$this->smarty->assign("data", $data);
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->smarty->assign("pagination", $pagination);

		// $this->smarty->assign("menu", "orders");
		$this->smarty->display("orders/index.html");
	}

	function detail($order_id){
		$this->load->model('Order_model');
		$order_info = $this->Order_model->get_order_by_id($order_id);
		$this->smarty->assign("menu", "orders");
		$this->smarty->assign("order_info", $order_info);
		$this->smarty->display("orders/detail.html");
	}

	/**
	 * 直播订单列表
	 */
	public function zhibo($status='all',$order_type = 2,$search=0,$st = 0,$et = 0,$limit = 0)
	{
		$this->offset = 10;
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post();
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$st = isset($post_data['st']) ? $post_data['st'] : "";
			$et = (isset($post_data['et'])) ? $post_data['et'] : "";
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 8;
		}
		$limit = $limit*$this->offset;
		$data = array( 'status' => $status,'order_type' => $order_type, 'search' => $search,'st' => $st,'et' => $et,'limit' => $limit);

		$this->load->model('Order_model');
		$result = $this->Order_model->get_order_list_by_school($limit,$this->offset,$this->user_data['school_id'],$data);
		$this->smarty->assign("order", $result['result']);
		//分页
		$this->load->library('pagination');
		$page_config['base_url'] = '/orders/index/'.$data['status'].'/'.$data['order_type'].'/'.$data['search'].'/'.$data['st'].'/'.$data['et'];
		$page_config['total_rows'] = $result['total_count'];
		$page_config['per_page'] = $this->offset;
		$this->smarty->assign("data", $data);
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->smarty->assign("pagination", $pagination);

		// $this->smarty->assign("menu", "orders");
		$this->smarty->display("orders/zhibo.html");
	}

	function change_order_price($order_id=0){
		if(empty($order_id)){
			echo json_encode(array("error" => 1, "msg" => "缺少订单号"));
			exit;
		}
		$order_info = get_field_by_where("shopping_orders", "*", "order_id = '{$order_id}'");
		if(!$order_info){
			echo json_encode(array("error" => 1, "msg" => "订单数据不存在"));
			exit;
		}
		if($order_info['order_status'] != 0){
			if($order_info['order_status'] < 0){
				$msg = '用户已取消该订单，无法修改价格';
			}
			if($order_info['order_status'] > 0){
				$msg = '用户已支付该订单，无法修改价格';
			}
			echo json_encode(array("error" => 1, "msg" => $msg));
			exit;
		}

		if($_POST){
			$post_data = $this->input->post();
			if(empty($post_data['total_order_amount'])){
				echo json_encode(array("error" => 1, "msg" => '金额不能为空'));
				exit;
			}
			if(empty($post_data['desc'])){
				echo json_encode(array("error" => 1, "msg" => '描述信息不能为空'));
				exit;
			}
			$this->db->where("order_id", $order_id);
			$this->db->update("shopping_orders", array("total_order_amount" => $post_data['total_order_amount'], "total_product_amount" => $post_data['total_order_amount'], "payment_money_amount" => $post_data['total_order_amount'], "total_order_commission" => $post_data['total_order_commission']));

			$history_data['order_id'] = $order_id;
			$history_data['desc'] = $post_data['desc'];
			$history_data['teacher_id'] = $this->user_data['teacher_id'];
			$history_data['teacher_name'] = $this->user_data['user_name'];
			$history_data['add_time'] = time();
			$this->db->insert("change_order_price_historys", $history_data);
			if($this->db->insert_id() > 0){
				echo json_encode(array("error" => 0, "msg" => '订单价格修改成功'));
				exit;
			}else{
				echo json_encode(array("error" => 1, "msg" => '修改失败，请重试'));
				exit;
			}
		}
		$this->smarty->assign("order_id", $order_id);
		$this->smarty->assign("order_info", $order_info);
		$this->smarty->display("orders/change_order_price.html");
	}

	function get_amount($type = 1){
		$post_data = $this->input->post();
		if($post_data){
			$mobile = isset($post_data['mobile']) ? $post_data['mobile'] : 0;
			$start_time = !empty($post_data['start_time']) ? $post_data['start_time'] : 0;
			$end_time = !empty($post_data['end_time']) ? $post_data['end_time'] : 0;
		}
		$this->load->model('Order_model');
		$conditions['school_id'] = $this->user_data['school_id'];
		$conditions['order_type'] = $type;
		$conditions['mobile'] = $mobile;
		$conditions['start_time'] = $start_time;
		$conditions['end_time'] = $end_time;
		$result = $this->Order_model->get_order_total_amount($conditions);
		echo number_format($result['total_amount'], 2, '.', '');
	}

	function del($order_id){
        $order_info = get_field_by_where("shopping_orders", "*", "order_id = '{$order_id}'");
        if(!$order_info){
            show_message($_SERVER['HTTP_REFERER'], "订单不存在");exit;
        }
        if($order_info['school_id'] != $this->user_data['school_id']){
            show_message($_SERVER['HTTP_REFERER'], "该订单不是你的，无法删除");exit;
        }
        if($order_info['order_status'] > 0){
            show_message($_SERVER['HTTP_REFERER'], "该订单已付款，无法删除");exit;
        }
        $this->db->where("order_id", $order_id);
        $this->db->delete("shopping_orders");
        if($this->db->affected_rows() > 0){
            show_message($_SERVER['HTTP_REFERER'], "订单删除成功");exit;
        }else{
            show_message($_SERVER['HTTP_REFERER'], "订单删除失败，请重试");exit;
        }
    }
}